﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace winApp2
{
    class DB
    {
        public static string ConnString = "";
        //根据库获取连接
        public static SqlConnection Conn(string db)
        {
            return new SqlConnection(ConnString+db);
        }
        //在库中执行Sql
        //getTable("mybookshop","select * from books")
        public static DataTable GetTable(string db, string sql)
        {
            SqlDataAdapter dap = new SqlDataAdapter(sql, Conn(db));
            DataTable dt = new DataTable();
            dap.Fill(dt);
            return dt;
        }
        //用于执行增加，删除改
        public static int ExecuteUpdate(string db, string sql)
        {
            SqlCommand cmd = new SqlCommand(sql, Conn(db));
            cmd.Connection.Open();
            try
            {
                return cmd.ExecuteNonQuery();
            }
            catch
            {
                throw;
            }
            finally
            {
                cmd.Connection.Close();
            }
        }

    }
}
